package com.speechb.controller;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.speechb.controller.base.BaseController;
import com.speechb.entity.Menu;
import com.speechb.service.IMenuService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author llj
 * @since 2022-04-20
 */
@RestController
@RequestMapping("/menu")
public class MenuController extends BaseController {
    @Autowired
    IMenuService menuService;

    @ResponseBody
    @RequestMapping("/select")
    public void select() {
        String search = getPara("search");
        int rows = getInt("rows");
        int page = getInt("page");
        if (StringUtils.isNotEmpty(search)) {
            search = "%" + search + "%";
        }
        List<Menu> menuList;
        try {
            Page<Object> pageHelper = PageHelper.startPage(page, rows, true);
            menuList = menuService.search(search);
            setAttr("data", menuList);
            setAttr("next_page", page + 1);
            setAttr("total", pageHelper.getTotal());
            renderUnCompressSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            renderDBError();
        }
    }
}
